HeroEquipLayerFunc={}
HeroEquipLayer={}
HeroEquipLayer.NodeID = {ArmyTable=1001, HeadBK=1003}
HeroEquipLayer.HeroItemID = {ImgBK=1001, Name=1002, Level=1003, BingZhong=1004}

HeroEquipLayerFunc.__index=HeroEquipLayerFunc
setmetatable(HeroEquipLayer, HeroEquipLayerFunc)

HeroEquipLayerFunc.create = function(self, target, action)
  local heroEquipLayer = {}
  heroEquipLayer.target = target
  heroEquipLayer.action = action
  setmetatable(heroEquipLayer, HeroEquipLayerFunc)
  heroEquipLayer.node = CCBReader:load("HeroEquipLayer.ccbi")
  heroEquipLayer:initHeroTable()
  --heroEquipLayer.currentHeroID = 1 
  --heroEquipLayer:showEquip()
  --heroEquipLayer:setTableFocusById(1)
  if HeroEquipLayer.oldHeroId~=nil then
    --heroEquipLayer:setTableFocus(HeroEquipLayer.oldHeroId)
    heroEquipLayer:setTableFocusById(HeroEquipLayer.oldHeroId)
  else
    heroEquipLayer:setTableFocus(1)
  end
  --heroEquipLayer:setTableFocusById(125)
  return heroEquipLayer
end

HeroEquipLayerFunc.initHeroTable = function(self)
  local armys = UserData.userinfo.host.armyGroups
  --[[local sortedArmys = {}
  for k,v in pairs(armys) do
    sortedArmys[#sortedArmys+1] = {id=tonumber(k), level=v.l}
  end
  local sortFunc = function(a, b) return b.level < a.level end
  table.sort(sortedArmys, sortFunc)
  ]]
  --for k,v in pairs(sortedArmys) do
  --  print(k, v.id, v.level)
  --end
  local sortedArmys = General:getGenerals()
  local items = CCArray:create()
  for k,v in ipairs(sortedArmys) do
    --print("initArmygroupTable", k, v)
    local item = CCBReader:load("HeroItem.ccbi")
    local info = UserData.config.models.General[tonumber(v.id)]
    --local sp = CCSprite:create(string.format("%s.png", info.asset))
    --local imgName = string.format("%d.png",v.id%40)
    local imgName = string.format("%s_100.png",info.asset)
    if tonumber(v.id)==1 then
      imgName = nameFromPath(UserData.userinfo.host.headpic)
    end
    local button = CCButton:createWithFileName("onHeroButton", imgName, imgName, imgName, "")
    local imgBK = item:getChildByTag(HeroEquipLayer.HeroItemID.ImgBK)
    button:setPosition(imgBK:getPosition())
    button:setTag(v.id)
    setObjectExtTable(button, self)
    item:addChild(button)

    local lb = item:getChildByTag(HeroEquipLayer.HeroItemID.Name)
    TypeCast(lb, "CCLabelTTF"):setString(info.name)
    if info.quality=="green" then
      lb:setColor(ccc3(0,255,0))
    elseif info.quality=="blue" then
      lb:setColor(ccc3(0,0,255))
    else
      lb:setColor(ccc3(255,255,255))
    end
    lb = item:getChildByTag(HeroEquipLayer.HeroItemID.BingZhong)
    TypeCast(lb, "CCLabelTTF"):setString(info.desc)
    lb = item:getChildByTag(HeroEquipLayer.HeroItemID.Level)
    TypeCast(lb, "CCLabelTTF"):setString(string.format("Lv%d", v.level))

    item:setContentSize(CCSizeMake(300,127))
    items:addObject(item)
  end
  --print("HeroEquipLayerFunc.initHeroTable items", items:count())
  local tableBK = self.node:getChildByTag(HeroEquipLayer.NodeID.ArmyTable)
  local size = tableBK:getContentSize()
  local tableview = CCTableView:create(0, CCSizeMake(size.width-20, size.height), items)
  local x, y = ActionUtil.leftBottomPoint(tableBK)
  tableview:setPosition(x+10, y)
  self.node:addChild(tableview)
  self.heroTable = tableview
  self.armys = sortedArmys 
end
HeroEquipLayerFunc.setTableFocusById = function(self, id)
  local index = -1
  for k,v in pairs(self.armys) do
    if v.id==id then index=k end
  end
  if index>0 then
    self:setTableFocus(index)
  end
end
HeroEquipLayerFunc.setTableFocus = function(self, index)
  print("HeroEquipLayerFunc.setTableFocus", index)
  for k,v in pairs(self.armys) do
    print(k, "---", v)
  end
  print("HeroEquipLayerFunc.setTableFocus", index, self.armys[index].id)
  local size = TypeCast(self.heroTable,  "CCTableView"):getItemSize()
  local tx = (index-1)*size.width
  local offset = TypeCast(self.heroTable,"CCScrollView"):getContentOffset()
  TypeCast(self.heroTable,"CCScrollView"):setContentOffset(ccp(-tx, offset.y))
  
  self.currentHeroID = self.armys[index].id
  self:showEquip()
end
HeroEquipLayerFunc.setEnable = function(self, enable)
  self.heroTable:setTouchEnable(enable)
end

HeroEquipLayerFunc.showEquip = function(self)
  HeroEquipLayer.oldHeroId = self.currentHeroID
  local heroid = self.currentHeroID
  --print("HeroEquipLayerFunc.showEquip", heroid)

  --设置头像
  if self.headImg~=nil then
    self.node:removeChild(self.headImg,true)
  end
  local spBK = self.node:getChildByTag(HeroEquipLayer.NodeID.HeadBK)
  local info = UserData.config.models.General[tonumber(heroid)] 
  local imgName = string.format("%s_100.png",info.asset)
  local sp = CCSprite:create(imgName)
  sp:setPosition(spBK:getPosition())
  self.node:addChild(sp)
  self.headImg = sp

  --设置装备
  if self.equipButtons~=nil then
  for i=0,self.equipButtons:size()-1 do
    local b = self.equipButtons:at(i)
    if b~=nil then
      self.node:removeChild(b,true)
    end
  end
  end

  self.equipButtons = Array:create()
  local equipIDs = UserData.userinfo.host.equips
  local equipIndexs = UserData.userinfo.host.armyGroups[tostring(heroid)].e
  for k,v in ipairs(equipIndexs) do
    --print("self.showEquip", k, v)
    if v>0 then
      local eid = equipIDs[tostring(v)].e
      local einfo = UserData.config.models.Equip[eid]
      local imgName = nameFromPath(einfo.asset)
      --print("ZhuangBeiLayer3.showEquip", v, eid, imgName)
      local bkImg = self.node:getChildByTag(10000+k)
      local button = CCButton:createWithFileName(self.action, imgName, imgName, imgName, "")
      setObjectExtTable(button, self.target)
      button:setTag(v)
      button:setPosition(bkImg:getPosition())
      self.node:addChild(button)
      self.equipButtons:append(button)
    end
  end
end

HeroEquipLayerFunc.onHeroButton = function(self, node, event)
  self.currentHeroID = node:getTag()
  self:showEquip()
  if ZBQHLayer.instance~=nil then 
    ZBQHLayer.instance:clearEquipInfo()
  end
end
